package com.wdb.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: wendaobai@qq.com
 * @date: 2024/1/10 0010 15:15
 */
@Configuration
@EnableWebMvc
public class ConfigurerAdapter implements WebMvcConfigurer {

  /** 文件配置 */
  private final FileProperties properties;

  public ConfigurerAdapter(FileProperties properties) {
    this.properties = properties;
  }

  @Bean
  public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOriginPattern("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    FileProperties.ElPath path = properties.getPath();
    String avatarUtl = "file:" + path.getAvatar().replace("\\", "/");
    String pathUtl = "file:" + path.getPath().replace("\\", "/");
    registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
    registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
    registry
        .addResourceHandler("/**")
        .addResourceLocations("classpath:/META-INF/resources/")
        .setCachePeriod(0);
  }

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    // 使用 fastjson 序列化，会导致 @JsonIgnore 失效，可以使用 @JSONField(serialize = false) 替换
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    List<MediaType> supportMediaTypeList = new ArrayList<>();
    supportMediaTypeList.add(MediaType.APPLICATION_JSON);
    FastJsonConfig config = new FastJsonConfig();
    config.setDateFormat("yyyy-MM-dd HH:mm:ss");
    config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
    converter.setFastJsonConfig(config);
    converter.setSupportedMediaTypes(supportMediaTypeList);
    converter.setDefaultCharset(StandardCharsets.UTF_8);
    converters.add(converter);
  }
}
